home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group93b.txt / 000004_icon-group-sender _Fri Apr 2 07:56:52 1993.msg < prev    next >
Internet Message Format  |  1993-06-16  |  3KB

  1. Received: from owl.CS.Arizona.EDU by cheltenham.CS.Arizona.EDU; Wed, 21 Apr 1993 18:47:50 MST
  2. Received: by owl.cs.arizona.edu; Wed, 21 Apr 1993 18:47:48 MST
  3. Date: 2 Apr 93 07:56:52 GMT
  4. From: ftpbox!mothost!binford!mcdchg!tellab5!ruth.wheaton.edu!gmribeir@uunet.uu.net  (Glauber)
  5. Organization: The Bossa Nova University
  6. Subject: Is this passable code?
  7. Message-Id: <1993Apr2.075652.11967@wheaton.wheaton.edu>
  8. Sender: icon-group-request@cs.arizona.edu
  9. To: icon-group@cs.arizona.edu
  10. Status: R
  11. Errors-To: icon-group-errors@cs.arizona.edu
  12.  
  13.  
  14. My apologies if this is repeated, but this is the 3rd time
  15. that i'm trying to post this. News sometimes behaves weird
  16. here.
  17.  
  18. I started using Icon recently, and am impressed with the
  19. power of the language. But, since i'm learning on my own and
  20. don't have experience with Icon, i don't know if i'm doing
  21. things right. Recently i wrote a short program to find
  22. anagrams (yeah...) using the unix dictionary. I'd appreciate
  23. any comments/suggestions/flames, etc about my programming
  24. style and the Icon way.
  25.  
  26.                     Thank you very much
  27.  
  28.                         Glauber.
  29.  
  30.  
  31. ----------------------------------------------------------------------
  32. # Find all the anagrams of a given word
  33. #    In the Unix dictionary
  34. # Tue Mar 30 07:15:53 CST 1993
  35.  
  36.  
  37.  
  38. procedure main(argv)
  39.  
  40.    word := argv[1]
  41.  
  42.    if not ( in := open("/usr/dict/words") )
  43.       then stop("Can't open system dictionary! :-( ")
  44.  
  45.    #initialize
  46.    l := map( read(in), &ucase, &lcase )
  47.  
  48.    #main loop
  49.    every ( w := lexperm( word ) ) do {
  50.       while ( l << w ) do 
  51.      if not ( l := map( read(in), &ucase, &lcase ) )
  52.         then return
  53.  
  54.       # at this point, either (l == w) or (l >> w)
  55.       if (l == w) then write(l)
  56.    }
  57. end
  58. #I think this is it!
  59.  
  60.  
  61.  
  62.  
  63. # Lexicographical permutation, 
  64. #    Reingold, Combinatorial Algorithms, page 162
  65. # (this is a generator)
  66.  
  67. procedure lexperm( w )
  68.    # initialize
  69.    w := sortword ( map(w, &ucase, &lcase) )
  70.    repeat {
  71.       suspend (w)
  72.       # now permute
  73.       every  i := (*w - 1) to 1 by (-1)  do  {
  74.      if w[i] << w[i+1] then break
  75.       }
  76.       every  j := *w to 1 by (-1)  do  {
  77.      if w[i] << w[j]   then break
  78.       }
  79.       w[i] :=: w[j]
  80.       r := *w
  81.       s := i + 1
  82.       while r >= s  do  {
  83.      w[r] :=: w[s]
  84.      r -:= 1
  85.      s +:= 1
  86.       }
  87.       # end of permutation
  88.       if (j = 1) & (i = 1) then fail
  89.    }
  90. end
  91.  
  92.  
  93.  
  94. # insertion sort for now...
  95. procedure sortword( w )
  96.    size := *w
  97.    # external loop, traverses the word
  98.    every j := 2 to size do {
  99.       hold := w[j]
  100.       # internal loop, "sinks" each letter in place
  101.       i := j - 1
  102.       while i >= 1  do  {
  103.      if hold >>= w[i] then break
  104.      w[i+1] := w[i]
  105.      i -:= 1
  106.       } 
  107.       w[i+1] := hold
  108.    }
  109.    return w
  110. end
  111.  
  112. ----------------------------------------------------------------------
  113.  
  114.  
  115.  
  116.  
  117. -- 
  118. Glauber Ribeiro
  119. glauber@david.wheaton.edu
  120. glauber%david.wheaton.edu@tellab5.tellabs.com
  121. constantly risking absurdity
  122.